const { Controller } = require("egg");

class VideoController extends Controller {
  // 获得视频列表
  async getVideoList() {
    const { ctx, app } = this;
    let page = ctx.query.page ? parseInt(ctx.query.page) : 1;
    let limit = ctx.query.limit ? parseInt(ctx.query.limit) : 3;
    console.log(page, limit);
    const offset = (page - 1) * 3;
    const id = ctx.authUser.id;
    // 查询视频列表 包含发布者信息与点赞和评论列表
    const videos = await app.model.Video.findAll({
      include: [
        {
          model: app.model.User,
          attributes: ["id", "username", "nickname", "avatar"],
        },
        {
          model: app.model.VideoLikes,
          attributes: ["id", "user_id"],
        },
        {
          model: app.model.VideoComment,
          attributes: ["id", "user_id", "content", "created_at"],
          include: [
            {
              model: app.model.User,
              attributes: ["id", "username", "nickname", "avatar"],
            },
          ],
        },
      ],
      offset,
      limit,
      order: [["id", "DESC"]],
    });
    // 获得当前用户的关注列表
    const followings = await app.model.VideoFollows.findAll({
      where: {
        user_id: id,
      },
      attributes: ["to_user_id"],
    });
    // 整理返回的数据格式
    const data = videos.map((video) => {
      return {
        id: video.id,
        title: video.title,
        cover: video.cover,
        url: video.url,
        user: video.user,
        isLiked: video.video_likes.some((item) => item.user_id === id),
        likesNum: video.video_likes.length,
        videoComments: video.video_comments || [],
        isFollow: followings.some((item) => item.to_user_id === video.user.id),
      };
    });
    ctx.success(data);
  }
  // 点赞和取消点赞
  async likeVideo() {
    const { ctx, app } = this;
    const id = ctx.authUser.id;
    // 参数验证
    ctx.validate({
      video_id: {
        type: "int",
        required: true,
        desc: "视频id",
      },
    });
    const videoId = ctx.params.video_id;
    // 判断是否已经点赞
    const isLike = await app.model.VideoLikes.findOne({
      where: {
        user_id: id,
        video_id: videoId,
      },
    });
    if (isLike) {
      // 取消点赞
      await isLike.destroy();
      ctx.success("取消点赞成功");
    } else {
      // 点赞
      const likes = await app.model.VideoLikes.create({
        user_id: id,
        video_id: videoId,
      });
      ctx.success(likes);
    }
  }
  // 关注和取消关注
  async followUser() {
    const { ctx, app } = this;
    const id = ctx.authUser.id;
    // 参数验证
    ctx.validate({
      to_id: {
        type: "int",
        required: true,
        desc: "用户id",
      },
    });
    const { to_id } = ctx.params;
    // 查找用户以前关注这个用户没有
    const isFollow = await app.model.VideoFollows.findOne({
      where: {
        user_id: id,
        to_user_id: to_id,
      },
    });
    if (isFollow) {
      // 取消关注
      await isFollow.destroy();
      ctx.success("取消关注成功");
    } else {
      // 关注
      const follows = await app.model.VideoFollows.create({
        user_id: id,
        to_user_id: to_id,
      });
      ctx.success(follows);
    }
  }
  // 获得关注列表
  async getFollowList() {
    const { ctx, app } = this;
    const id = ctx.authUser.id;
    // 获得关注列表
    const follows = await app.model.VideoFollows.findAll({
      where: {
        user_id: id,
      },
      include: [
        {
          model: app.model.User,
        },
      ],
    });
    ctx.success(follows);
  }
  // 新增视频评论
  async addVideoComment() {
    const { ctx, app } = this;
    const id = ctx.authUser.id;
    // 参数验证
    ctx.validate({
      video_id: {
        type: "int",
        required: true,
        desc: "视频id",
      },
      content: {
        type: "string",
        required: true,
        desc: "评论内容",
      },
    });
    const { video_id, content } = ctx.request.body;
    // 新增评论
    const comment = await app.model.VideoComment.create({
      user_id: id,
      video_id,
      content,
      reoly_id: 0,
    });
    if (!comment) {
      ctx.throw(500, "评论失败");
    }
    ctx.success(comment);
  }
}
module.exports = VideoController;
